package position

import "fmt"

/**
持仓数据
 */
type HoldPosition struct {
	Cost int64 //持仓成本
	Num int64 //持仓数量
}

/**
初始化持仓
 */
func (hold *HoldPosition) First (cost int64, num int64) {
	hold.Cost = cost
	hold.Num = num

	fmt.Println("初始持仓成本：", float64(hold.Cost) / 100 ," 元，股数：" , hold.Num)
}

/**
买入后更新持仓数据
 */
func (hold *HoldPosition) Buy (buyPrice int64, buyNum int64) {
	total := hold.Cost * hold.Num + buyPrice * buyNum

	hold.Num += buyNum
	hold.Cost = total / hold.Num
}

/**
卖入后更新持仓数据
 */
func (hold *HoldPosition) Cell (cellPrice int64, cellNum int64) {
	total := hold.Cost * hold.Num - cellPrice * cellNum

	hold.Num -= cellNum
	if hold.Num == 0 {
		hold.Cost = 0
	} else {
		hold.Cost = total / hold.Num
	}
}

/**
清仓
 */
func (hold *HoldPosition) Empty() {
	hold.Num = 0
	hold.Cost = 0
}